<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" class="top">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="请输入商品名称"
            v-model="queryinfo.query"
            size="small"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="query()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="gosave()"
            >添加</el-button
          >
        </el-col>
      </el-row>

      <el-row>
        <el-table class="tabletop" :data="tableData" border style="width: 100%">
          <el-table-column type="index" label="序号"></el-table-column>
          <el-table-column
            prop="goods_name"
            label="商品名称"
            width="200"
          ></el-table-column>
          <el-table-column
            prop="goods_price"
            label="商品价格"
          ></el-table-column>
          <el-table-column
            prop="goods_number"
            label="商品数量"
          ></el-table-column>
          <el-table-column prop="add_time" label="创建时间">
            <template slot-scope="scope" width="360">
              <div v-html="$options.filters.gltime(scope.row.add_time)"></div>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="goeait(scope.row)"
              ></el-button>
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="del(scope.row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryinfo.pagesize"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalsize"
        ></el-pagination>
      </el-row>
    </el-card>
    <el-dialog title="修改商品信息" :visible.sync="isShow">
      <el-form :model="goodssaveobj">
        <el-form-item label="商品ID">
          <el-input
            v-model="goodssaveobj.goods_id"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input
            v-model="goodssaveobj.goods_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input
            v-model="goodssaveobj.goods_price"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品数量">
          <el-input
            v-model="goodssaveobj.goods_number"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品重量">
          <el-input
            v-model="goodssaveobj.goods_weight"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "goods",
  data() {
    return {
      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      isShow: false,
      totalsize: 0,
      tableData: [],
      goodssaveobj: {
        goods_id: 0,
        goods_name: "",
        goods_number: 0,
        goods_price: 0,
        goods_weight: 0,
      },
    };
  },
  filters: {
    gltime: function (value) {
      const newdate = new Date(value);
      let str =
        newdate.getFullYear() +
        "-" +
        (newdate.getMonth() + 1) +
        "-" +
        newdate.getDate() +
        "<br />" +
        newdate.getHours() +
        ":" +
        newdate.getMinutes() +
        ":" +
        newdate.getSeconds();
      return str;
    },
  },
  methods: {
    async query() {
      let rst = await this.$http.get("goods", this.queryinfo);
      if (rst.meta.status == 200) {
        this.totalsize = rst.data.total;
        this.tableData = rst.data.goods;
      } else {
        this.$message.error("获取商品列表失败");
      }
    },
    handleSizeChange(size) {
      this.queryinfo.pagesize = size;
      this.query();
    },
    handleCurrentChange(pagenum) {
      this.queryinfo.pagenum = pagenum;
      this.query();
    },
    gosave() {
      this.$router.push("/home/goodsadd");
    },
    async goeait(row) {
      this.goodssaveobj.goods_id = row.goods_id;
      this.goodssaveobj.goods_name = row.goods_name;
      this.goodssaveobj.goods_number = row.goods_number;
      this.goodssaveobj.goods_price = row.goods_price;
      this.goodssaveobj.goods_weight = row.goods_weight;
      this.isShow = true;
    },
    async save() {
      let rst = await this.$http.put(
        `goods/${this.goodssaveobj.goods_id}`,
        this.goodssaveobj
      );
      if (rst.meta.status == 200) {
        this.query();
        this.isShow = false;
      } else {
        this.$message.error("保存失败");
      }
    },
    del(row) {
      this.$confirm(`此操作将永久删除该文件，是否继续？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let rst = await this.$http.delete(`goods/${row.goods_id}`);
          console.log(rst);
          if (rst.meta.status == 200) {
            this.query();
          } else {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
  },
  mounted() {
    this.query();
  },
};
</script>

<style>
.top {
  margin-bottom: 20px;
}
</style>